<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<HTML>

<HEAD>
  <TITLE>Elsa FAQ</TITLE>
  <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  <style type="text/css">
    H1 { font-size: 150% }
    H2 { font-size: 125% }
    H3 { font-size: 100% }
    P.title { font-size: 175% }
    a.toc:link { text-decoration: none }
    a.toc:visited { text-decoration: none }
  </style>
</HEAD>

<body>

<center>
<p class="title"><b>Elsa Frequently Asked Questions</b></p>
</center>

<p>
This page has answers to questions that I've received and think
might be useful generally.

<h1>Contents</h1>

<p>
<!-- BEGIN CONTENTS -->
<!-- automatically generated by insert-html-toc; do not edit the TOC directly -->
<ul>
  <li><a class="toc" href="#dependent_base_names">1. Why can't Elsa find names in template base classes?</a>
</ul>
<!-- END CONTENTS -->

<a name="dependent_base_names"></a>
<h1>1. Why can't Elsa find names in template base classes?</h1>

<p>
Given the input:
<pre>
  // foo.cc

  template &lt;class T&gt;
  class Base {
  public:
    int x;
  };

  template &lt;class T&gt;
  class Derived : public Base&lt;T&gt; {
  public:
    int get_x() {
      return x;            // line 13
    }
  };
</pre>

<p>
Elsa (invoked via ccparse) says:
<pre>
  foo.cc:13:12: error: there is no variable called `x' (from template; would be suppressed in permissive mode)
  typechecking results:
    errors:   1
    warnings: 0
</pre>

<p>
The reason Elsa rejects this input is that it is doing what
is usually called
<a href="http://www.google.com/search?q=two+phase+lookup+c%2B%2B">"two phase lookup"</a>,
officially known as non-dependent name resolution, in template bodies.
These rules are specified in the C++ Standard, section 14.6.

<p>
Basically, since the name <tt>x</tt> does not appear to depend on the
template arguments, it is looked up at the time the template
definition is being processed.  Further, at definition time, we cannot
know which specialization of <tt>Base</tt> will be used (since that
<emph>is</emph> dependent on the template arguments), so none of the
names from <tt>Base</tt> are visible.  Consequently, no declaration for
<tt>x</tt> can be found.

<p>
As the diagnostic indicates, this error is suppressed in permissive
mode (say <tt>-tr permissive</tt> to <tt>ccparse</tt>) for compatibility
with gcc prior to version 3.4.

<p>
To fix the code, change it to:
<pre>
    int get_x() {
      return this-&gt;x;            // line 13
    }
</pre>

<p>
This change has the effect of making <tt>x</tt> depend on the template
arguments (since <tt>this</tt> inside a template class is considered
dependent), and hence lookup of <tt>x</tt> will be delayed until 
the template is instantiated, at which point we will know which <Tt>Base</Tt>
will be used and can therefore look inside it.  Assuming the <Tt>Base</Tt>
used is the one shown above, <tt>x</tt> will then be found.


</body>
</html>
